/**
 * 判断传入的参数是不是为null、undefined或者''
 * 不定量参数
 */
export const hasValue = function () {
  for (var i = 0; i < arguments.length; i++) {
    var v = arguments[i];
    if (isNull(v)) {
      console.log(" 🐣  check result: null");
      return false;
    }
    if (typeof (v) === "undefined") {
      console.log(" 🐣  check result: undefined");
      return false;
    }
    if (v === '') {
      console.log(" 🐣  check result: str length 0");
      return false;
    }
    if (v === "null") {
      console.log(" 🐣  check result: str is null");
      return false;
    }
  }
  console.log(" 🐣  check result: not empty");
  return true;
}

function isNull(v) {
  if (v === null) {
    return true;
  }
  return false;
}

export const convertObjectToArray = function (obj, label, value, exclude) {
  var count = 0;
  var result = [];
  if (!hasValue(label, value)) {
    label = 'key';
    value = 'value';
  }
  for (var key in obj) {
    if (hasValue(exclude) && exclude.indexOf(key) == -1)
      result[count++] = {
        label: key,
        value: obj[key]
      }
  }
  return result;
};




export const weekOfYear = function (d) {
    return moment(d).isoWeek();
};
/**
 * Created by HMH on 2017/7/13.
 */
/**
 *js中更改日期
 * y年， m月， d日， h小时， n分钟，s秒
 */
export const str2date = function (str, fmt) {
  fmt = fmt || 'yyyy-MM-dd';
  var obj = {y: 0, M: 1, d: 0, H: 0, h: 0, m: 0, s: 0, S: 0};
  fmt.replace(/([^yMdHmsS]*?)(([yMdHmsS])\3*)([^yMdHmsS]*?)/g, function (m, $1, $2, $3, $4, idx, old) {
    str = str.replace(new RegExp($1 + '(\\d{' + $2.length + '})' + $4), function (_m, _$1) {
      obj[$3] = parseInt(_$1);
      return '';
    });
    return '';
  });
  obj.M--; // 月份是从0开始的，所以要减去1
  var date = new Date(obj.y, obj.M, obj.d, obj.H, obj.m, obj.s);
  if (obj.S !== 0) date.setMilliseconds(obj.S); // 如果设置了毫秒
  return date;
};

//author: meizz
export const date2str = function (date, fmt) {
  const o = {
    "M+": date.getMonth() + 1,                 //月份
    "d+": date.getDate(),                    //日
    "h+": date.getHours(),                   //小时
    "m+": date.getMinutes(),                 //分
    "s+": date.getSeconds(),                 //秒
    "q+": Math.floor((date.getMonth() + 3) / 3), //季度
    "S": date.getMilliseconds()             //毫秒
  };
  if (/(y+)/.test(fmt))
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (const k in o)
    if (new RegExp("(" + k + ")").test(fmt))
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
};


export const getExpandName = function (fileName) {
  const index1 = fileName.lastIndexOf(".");
  const index2 = fileName.length;
  //后缀名
  return fileName.substring(index1 + 1, index2);
};

export const deleteTableHeader = function (arr) {
  for (let i = 1; i < arguments.length; i++) {
    removeArr(arr, 'prop', arguments[i]);
  }
};

export const indexOfArr = function (arr, field, value) {
  for (let j = 0; j < arr.length; j++) {
    if (value === arr[j][field]) {
      return j;
    }
  }
  return -1;
};

export const removeArr = function (arr, field, value) {
  let index = indexOfArr(arr, field, value);
  if (index !== -1) {
    arr.splice(index, 1);
  }
};

import store from '../store'

export const hasPrivileges = function (list) {
  let privileges = store.getters.userInfo.privileges;
  for (let i = 0; i < privileges.length; i++) {
    for (let j = 0; j < list.length; j++) {
      if (privileges[i] === list[j]) {
        return true;
      }
    }
  }
  return false;
};

import moment from 'moment'

export const getFirstDayOfWeek = function (week,year) {
  return moment().isoWeekday(1).week(week).year(year).toDate();
};

export const getWeekNumber = function(src) {
  const date = src;
  date.setHours(0, 0, 0, 0);
  // Thursday in current week decides the year.
  date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7);
  // January 4 is always in week 1.
  const week1 = new Date(date.getFullYear(), 0, 4);
  // Adjust to Thursday in week 1 and count number of weeks from date to week 1.
  // Rounding should be fine for Daylight Saving Time. Its shift should never be more than 12 hours.
  return 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000 - 3 + (week1.getDay() + 6) % 7) / 7);
};
